﻿namespace BaseClass.Duoc
{
    using ConfigDatabase.Class;
    using Npgsql;
    using System;
    using System.Data;

    public class dtoDCapMaVach
    {
        private ConnectDatabase _condata;
        private NpgsqlConnection _connect;
        private long _deid;
        private int _iidduocpham;
        private string _sngaynhap;
        private string _sSchema;

        public dtoDCapMaVach()
        {
            this.KhoiTao();
            this._condata = new ConnectDatabase();
            this._sSchema = this._condata.Schema;
        }

        public dtoDCapMaVach(ConnectDatabase conn)
        {
            this.KhoiTao();
            this._condata = conn;
            this._sSchema = this._condata.Schema;
        }

        public bool Delete()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Delete(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDCapMaVach.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Delete(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand("delete from " + this._sSchema + "." + tableName + " where 1=1  ", connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Delete()", "dtoDCapMaVach.cs");
                return false;
            }
        }

        public int GetIDInsert()
        {
            int iDInsert;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                iDInsert = this.GetIDInsert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert()", "dtoDCapMaVach.cs");
                iDInsert = 1;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return iDInsert;
        }

        public int GetIDInsert(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select max(id) idmax  from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                int num = 0;
                try
                {
                    num = Convert.ToInt32(dataTable.Rows[0][0].ToString());
                }
                catch
                {
                }
                return ++num;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "GetIDInsert(NpgsqlConnection connect)", "dtoDCapMaVach.cs");
                return 1;
            }
        }

        public bool Insert()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Insert(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDCapMaVach.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Insert(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "insert into ", this._sSchema, ".", tableName, "(", columnID, ",", columnIDDUOCPHAM, ") values(", this._deid, ",", this._iidduocpham, ")" }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Insert()", "dtoDCapMaVach.cs");
                return false;
            }
        }

        private void KhoiTao()
        {
            this._deid = 0L;
            this._iidduocpham = 0;
            this._sngaynhap = "";
        }

        public void Load()
        {
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                this.Load(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load()", "dtoDCapMaVach.cs");
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
        }

        public void Load(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDDUOCPHAM + ",to_char(" + columnNGAYNHAP + ",'dd/mm/yyyy') " + columnNGAYNHAP + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                this._deid = Convert.ToInt64(dataTable.Rows[0][columnID].ToString());
                this._iidduocpham = Convert.ToInt32(dataTable.Rows[0][columnIDDUOCPHAM].ToString());
                this._sngaynhap = dataTable.Rows[0][columnNGAYNHAP].ToString();
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Load(NpgsqlConnection connect)", "dtoDCapMaVach.cs");
            }
        }

        public DataTable LoadAll()
        {
            DataTable table;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                table = this.LoadAll(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll()", "dtoDCapMaVach.cs");
                table = null;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return table;
        }

        public DataTable LoadAll(NpgsqlConnection connect)
        {
            try
            {
                DataTable dataTable = new DataTable();
                new NpgsqlDataAdapter(" select  " + columnID + "," + columnIDDUOCPHAM + ",to_char(" + columnNGAYNHAP + ",'dd/mm/yyyy') " + columnNGAYNHAP + " from " + this._sSchema + "." + tableName + " ", connect).Fill(dataTable);
                return dataTable;
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "LoadAll(NpgsqlConnection connect)", "dtoDCapMaVach.cs");
                return null;
            }
        }

        public bool Update()
        {
            bool flag;
            try
            {
                this._connect = new NpgsqlConnection(this._condata.StringConnect);
                this._connect.Open();
                flag = this.Update(this._connect);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDCapMaVach.cs");
                flag = false;
            }
            finally
            {
                this._connect.Close();
                this._connect.Dispose();
            }
            return flag;
        }

        public bool Update(NpgsqlConnection connect)
        {
            try
            {
                NpgsqlCommand command = new NpgsqlCommand(string.Concat(new object[] { "update ", this._sSchema, ".", tableName, " set ", columnID, " = ", this._deid, ",", columnIDDUOCPHAM, " = ,", columnNGAYNHAP, " = to_timestamp('", this._sngaynhap, "','dd/mm/yyyy') " }), connect) {
                    CommandType = CommandType.Text
                };
                return (command.ExecuteNonQuery() != 0);
            }
            catch (NpgsqlException exception)
            {
                ConnectDatabase.TrackingError(exception.ToString(), "Update()", "dtoDCapMaVach.cs");
                return false;
            }
        }

        public static string columnID
        {
            get
            {
                return "id";
            }
        }

        public static string columnIDDUOCPHAM
        {
            get
            {
                return "idduocpham";
            }
        }

        public static string columnNGAYNHAP
        {
            get
            {
                return "ngaynhap";
            }
        }

        public long pID
        {
            get
            {
                return this._deid;
            }
            set
            {
                this._deid = value;
            }
        }

        public int pIDDUOCPHAM
        {
            get
            {
                return this._iidduocpham;
            }
            set
            {
                this._iidduocpham = value;
            }
        }

        public string pNGAYNHAP
        {
            get
            {
                return this._sngaynhap;
            }
            set
            {
                this._sngaynhap = value;
            }
        }

        public static string tableName
        {
            get
            {
                return "d_capmavach";
            }
        }
    }
}

